<template>
<el-dialog title="用户" :visible.sync="visible" v-if="visible" width="60%" append-to-body v-dialogDrag :close-on-click-modal="false">
  <div class="app-container">
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
    <el-tree
    :data="sysOrgList"
    show-checkbox
    node-key="orgId"
    accordion
    ref="tree"
    default-expand-all
    :filter-node-method = "filterNode"
    :props="defaultProps"
    :check-strictly = 'isLinked'
    >
  </el-tree>
  </div>
  <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="confirmUser">确认</el-button>
        <el-button size="small" @click="visible=false">取 消</el-button>
  </div>
</el-dialog>
</template>

<script>
import { getWorkflowUserListByPage } from "@/api/workflow/workflowUser";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "SysOrg",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  props: {
    // 回显的数据
    propSysOrgList: {
      type: Array,
      default:()=>[]
    },
    // 是否与子级联动 false为联动
    isLinked: {
      type: Boolean,
      default:false
    }
  },
  data() {
    return {
      defaultProps: {
          children: 'children',
          label: 'orgName'
      },
      // 遮罩层
      loading: true,
      visible: false,
      // 过滤字段
      filterText: null,
      // 表格树数据
      sysOrgList: [],
      //回显id
      orgIds: [],
      // 查询参数
      queryParams: {
        type: 'org'
      },
    };
  },

  watch: {
     propSysOrgList(val) {
       if(val.length>0){
          this.orgIds = []
          this.getList()
          val.forEach(orgId => {
            this.orgIds.push(orgId)
          })
       }else{
          this.orgIds = []
          this.getList()
       }
     },
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    //过滤
    filterNode(value, data) {
        if (!value) return true;
        return data.orgName.indexOf(value) !== -1;
    },
    /** 查询机构列表 */
    getList() {
      this.loading = true;
      getWorkflowUserListByPage(this.queryParams).then(response => {
        this.sysOrgList = this.handleTree(response.data.list, "orgId");
        this.loading = false;
        this.$nextTick(() => {
          if(this.orgIds.length > 0){
            this.$refs.tree.setCheckedKeys(this.orgIds);
          }
        })
      });
    },
    // 确认
    confirmUser(){
      let sysOrgList = this.$refs.tree.getCheckedNodes();
      if(sysOrgList.length>0){
        this.$emit("confirmUser",sysOrgList)
      }else{
        this.$modal.msgWarning("请选择机构！");
      }
    }
  }
};
</script>
